<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2019-07-04 01:36:52
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-07 05:33:00
 -->
<template>
  <el-form
    ref="loginForm"
    class="login_form"
    :model="loginForm"
    :rules="loginRules"
    auto-complete="on"
    label-position="left"
  >
    <el-form-item prop="username">
      <el-input
        ref="username"
        v-model.trim="loginForm.username"
        placeholder="用户名"
        name="username"
        type="text"
        auto-complete="on"
      >
        <template slot="prepend">
          <svg-icon icon-class="user" />
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        :key="passwordType"
        ref="password"
        v-model.trim="loginForm.password"
        :type="passwordType"
        placeholder="密码"
        name="password"
        auto-complete="on"
        @keyup.enter.native="handleLogin"
      >
        <template slot="prepend">
          <svg-icon icon-class="password" />
        </template>
        <template slot="append">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            @click="showPwd"
          />
        </template>
      </el-input>
    </el-form-item>
    <!-- <div id="c1">c1</div> -->
    <el-button
      class="margin-t20"
      :loading="loading"
      type="primary"
      size="medium"
      style="width:100%; margin-bottom:30px;"
      @click.native.prevent="btnLogin"
    >登 录</el-button>
  </el-form>
</template>
<script>

import config from '@/config/index.js'

export default {
  name: 'Login',
  data() {
    return {
      valicode: null,
      disabled: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入用户名！', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码！', trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    btnLogin() {
      this.handleLogin()
    },
    initData() {
      const appId = config.appId
      const _this = this
      // eslint-disable-next-line no-undef
      const valicode = _dx.Captcha(document.getElementById('c1'), {
        appId, // appId，在控制台中“应用管理”或“应用配置”模块获取
        style: 'inline',
        width: '330px',
        inlineFloatPosition: 'up',
        success: function(token) {
          _this.$set(_this.loginForm, 'token', token)
          _this.disabled = false
        }
      })
      this.valicode = valicode
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then((res) => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.valicode.reload()
            this.disabled = true
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/* reset element-ui css */
  .login_form /deep/ input {
    padding: 10px 20px;
    height: 40px;
  }
</style>
